﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using FinPlusAssembler;
using FinPlusInterfaces;

namespace FinPlusCompView
{
    public partial class FinPlusColButtonBar : FinPlusControl
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }
        public event EventHandler<IPod> ColButtonClick;

        //construct
        public FinPlusColButtonBar(IList<string> buttons)
        {
            InitializeComponent();
            foreach (var button in buttons)
                AddButton(button);

            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.ColButton: ColButtonClick.Invoke(this, command); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = Level.Warning.Log(Id, Config, e.Message, e);
            }
        }
    
        //private
        private void AddButton(string value)
        {
            var b = value.Split(',');
            var button = new Button() { Content = b[0], Tag = value };
            button.Style = (Style)this.FindResource("DarkButtonStyle");
            button.Click += Button_Click;
            buttonBar.Children.Add(button);
        }

        //cmds
        private enum Cmds { ColButton }

        //events
        private void Button_Click(object s, RoutedEventArgs a) { CmdRouter(Cmds.ColButton.ToString(), s); }
    }
}
